﻿using PagedList;
using RequirementAndBug.Common;
using RequirementAndBug.Common.Entities;
using RequirementAndBug.Common.Entities.RequireModels;
using RequirementAndBug.Common.Entities.RoleModels;
using RequirementAndBug.Common.Entities.UserModels;
using RequirementAndBug.Web.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RequirementAndBug.Web.Controllers
{
    public class BugController : BaseController
    {
        public ActionResult Index(string title, string stageStatus, string procategory, string targetUserID,string status="1",string pageIndex="1",string pageSize="50",string isSumbit="0")
        {

            ViewBag.UserName = Session["UserName"];

            RequireQueryConditionResponse response = new RequireQueryConditionResponse();

            //类型列表
            List<SelectListItem> proCategoryList = new List<SelectListItem>();
            proCategoryList.Add(new SelectListItem { Text = "全部", Value = "" });
            proCategoryList.Add(new SelectListItem { Text = "Bug", Value = "1" });
            proCategoryList.Add(new SelectListItem { Text = "需求", Value = "0" });

            ///状态列表
            List<SelectListItem> stageStatusList = new List<SelectListItem>();
            stageStatusList.Add(new SelectListItem { Text = "全部", Value = "" });
            stageStatusList.Add(new SelectListItem { Text = "已完成", Value = "3" });
            stageStatusList.Add(new SelectListItem { Text = "待验收", Value = "2" });
            stageStatusList.Add(new SelectListItem { Text = "待测试", Value = "1" });
            stageStatusList.Add(new SelectListItem { Text = "待开发", Value = "0" });

            ///状态列表
            List<SelectListItem> statusList = new List<SelectListItem>();            
            statusList.Add(new SelectListItem { Text = "正常", Value = "1" });
            statusList.Add(new SelectListItem { Text = "删除", Value = "0" });
            

            //人员列表
            List<UserInfo> userList = userService.GetNormalUserList();
            List<SelectListItem> itemList = new List<SelectListItem>();
            itemList.Add(new SelectListItem() { Text = "所有人", Value = "", Selected = true });
            foreach (var user in userList)
            {
                SelectListItem item = new SelectListItem() { Text = user.NickName, Value = user.UserID.ToString() };
                itemList.Add(item);
            }

            Hashtable hs = new Hashtable();
            hs.Add("PageIndex", pageIndex);
            hs.Add("PageSize", pageSize);

            if (!string.IsNullOrWhiteSpace(title))
            {
                hs.Add("Title", title);
                response.TitleKw = title;
            }

            if (!string.IsNullOrWhiteSpace(stageStatus))
            {
                hs.Add("StageStatus", stageStatus);
                stageStatusList.Find(s => s.Value == stageStatus).Selected = true;
            }


            if (!string.IsNullOrWhiteSpace(status))
            {
                hs.Add("Status", status);
                statusList.Find(s => s.Value == status).Selected = true;
            }
            


            if (!string.IsNullOrWhiteSpace(procategory))
            {
                hs.Add("Procategory", procategory);
                proCategoryList.Find(s => s.Value == procategory).Selected = true;
            }
            if (!string.IsNullOrWhiteSpace(targetUserID))
            {
                hs.Add("TargetUserID", targetUserID);
                itemList.Find(s => s.Value == targetUserID).Selected = true;
            }

            //List<RequireListItem> list = bugService.GetAllRequireList(hs);
            //response.StatusList = statusList;
            //response.ProCategoryList = proCategoryList;
            //response.UserList = itemList;
            //response.RequireList = list;            
            //return View(response);
            IPagedList<RequireListItem> list = bugService.GetAllRequireList(hs);
            response.StageStatusList = stageStatusList;
            response.StatusList = statusList;
            response.ProCategoryList = proCategoryList;
            response.UserList = itemList;
            response.RequireList = list;
            response.PageIndex =list.PageNumber;
            response.PageSize= list.PageSize;
            response.PageCount = list.PageCount;
            return View(response);
        }
                
        public ActionResult Create()
        {
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "需求", Value = "0" });
            list.Add(new SelectListItem() { Text = "Bug", Value = "1" });
            ViewBag.ProCategoryList = list;

            //人员列表
            List<UserInfo> userList = userService.GetNormalUserList();
            List<SelectListItem> itemList = new List<SelectListItem>();
            itemList.Add(new SelectListItem() { Text="所有人",Value="0",Selected=true });
            foreach (var user in userList)
            {
                SelectListItem item = new SelectListItem() { Text = user.NickName, Value = user.UserID.ToString() };
                itemList.Add(item);
            }

            ViewBag.TargetUserList = itemList;
            return View();
        }
        
        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Create(RequireDetailItem item)
        {
            if (ModelState.IsValid)
            {
                item.CreateTime = DateTime.Now;
                item.UserID = Convert.ToInt32(Session["UserID"]);

                int x = bugService.AddBugInfo(item);
                return RedirectToAction("Index", "Bug");
            }
            else
            {
                List<SelectListItem> list = new List<SelectListItem>();
                list.Add(new SelectListItem() { Text = "需求", Value = "0" });
                list.Add(new SelectListItem() { Text = "Bug", Value = "1" });
                ViewBag.ProCategoryList = list;

                List<UserInfo> userList = userService.GetNormalUserList();
                List<SelectListItem> itemList = new List<SelectListItem>();
                itemList.Add(new SelectListItem() { Text = "所有人", Value = "0", Selected = true });
                foreach (var user in userList)
                {
                    SelectListItem item2 = new SelectListItem() { Text = user.NickName, Value = user.UserID.ToString() };
                    itemList.Add(item2);
                }
                ViewBag.TargetUserList = itemList;
            }
            return View();
        }

        

        [HttpGet]
        public ActionResult Edit(string keyid)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "需求", Value = "0" });
            list.Add(new SelectListItem() { Text = "Bug", Value = "1" });
            ViewBag.ProCategoryList = list;

            //人员列表
            List<UserInfo> userList = userService.GetNormalUserList();
            List<SelectListItem> itemList = new List<SelectListItem>();
            itemList.Add(new SelectListItem() { Text = "所有人", Value = "0", Selected = true });
            foreach (var user in userList)
            {
                SelectListItem item = new SelectListItem() { Text = user.NickName, Value = user.UserID.ToString() };
                itemList.Add(item);
            }
            ViewBag.TargetUserList = itemList;

            if (string.IsNullOrWhiteSpace(keyid))
            {
                return View();
            }

            RequireDetailItem itemResp = bugService.GetRequireDetailInfo(Convert.ToInt32(keyid));
            return View(itemResp);
        }

        

        [HttpPost]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public ActionResult Edit(RequireDetailItem item)
        {
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Text = "需求", Value = "0" });
            list.Add(new SelectListItem() { Text = "Bug", Value = "1" });
            ViewBag.ProCategoryList = list;

            int x = bugService.EditBugInfo(item);
            return RedirectToAction("Index", "Bug");            
           
        }


        [HttpGet]
        public ActionResult Scan(string keyid)
        {
            if (string.IsNullOrWhiteSpace(keyid))
            {
                return View();
            }
            RequireScanDetailItem item = bugService.GetRequireScanDetailInfo(Convert.ToInt32(keyid));
            int userID = Convert.ToInt32(Session["UserID"]);
            int roleID = Convert.ToInt32(Session["RoleID"]);
            int stage = item.StageStatus;
            List<RoleButtonRight> list = bugService.GetRoleBtnList(roleID);   
            if (item.UserID == userID)
            { 
                //如果是本人，多一个编辑按钮
                RoleButtonRight subBtn = new RoleButtonRight();
                subBtn.BtnName = "重新编辑";
                subBtn.BtnTriggerScript = "GoToEdit";
                subBtn.ShowBtnStage = new List<int> { 0, 1, 2 };
                list.Add(subBtn);                
            }

            List<RoleButtonRight> btnRightList = new List<RoleButtonRight>();
            //获取当前阶段显示的按钮
            foreach (var btn in list)
            {
                if (btn.ShowBtnStage.Exists(s => s == stage))
                {
                    btnRightList.Add(btn);  
                }            
            }

            ViewBag.BtnRightList = btnRightList;
            return View(item);
        }

        [AuthenticationAttribute(IsUsedAttribute = false)]
        [HttpPost]        
        public JsonResult BugStageChange(ChangeStageRequest request)
        {            
            ResultInfo r;        
            if (string.IsNullOrWhiteSpace(request.StageTitle))
            {
                r = new ResultInfo(RT.NoSuccess.Code, RT.NoSuccess.Msg);
                return Json(r);
            }          

            string stageTitle = request.StageTitle;
            int requireID = request.RequireID;
            int x = 0;
            if (stageTitle == EBugStatus.Developing.ToString())
            {
                x = bugService.BugStageChange(EBugStatus.Developing, requireID);
            }
            else if (stageTitle == EBugStatus.Testing.ToString())
            {
                x = bugService.BugStageChange(EBugStatus.Testing, requireID);
            }
            else if (stageTitle == EBugStatus.Accepting.ToString())
            {
                x = bugService.BugStageChange(EBugStatus.Accepting, requireID);
            }
            else if (stageTitle == EBugStatus.Finish.ToString())
            {
                x = bugService.BugStageChange(EBugStatus.Finish, requireID);
            }

            if (x > 0)
            {
                r = new ResultInfo(RT.Success.Code, RT.Success.Msg);
                return Json(r);
            }
            else
            {
                r = new ResultInfo(RT.NoSuccess.Code, RT.NoSuccess.Msg);
                return Json(r);
            }        
        
        }

        [AuthenticationAttribute(IsUsedAttribute = false)]
        [HttpPost]
        public JsonResult Delete(RequireDeleteRequest request)
        {
            int x = bugService.DeleteBugInfo(request);
            dynamic myResult = new System.Dynamic.ExpandoObject();
            myResult.Code = 1;
            myResult.Msg = "请求成功";
            myResult.Data = null;
            return Json(myResult);
        }
    }
}
